package com.aaa.dem1109.作业;

import com.sun.corba.se.spi.ior.MakeImmutable;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @Author: whj
 * @Version 1.0
 * @Date: 2022/11/9  19:19
 */
public class ProxyFactory implements MethodInterceptor {
    //目标类--被代理的对象
    private Object target;
    public ProxyFactory(Object target) {
        this.target = target;
    }

    //获取代理类
    public Object getTarget(){
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(target.getClass());
        enhancer.setCallback(this);
        return enhancer.create();
    }

    @Override
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
        pre();
        Object invoke = method.invoke(target,args);
        System.out.println("输出计算结果:"+invoke);
        after();
        return invoke;
    }
    //前置校验内容
    public void pre(){
        System.out.println("这是前置校验内容");
    }

    //结束之后的日志内容
    public void after(){
        System.out.println("添加日志内容");
    }
}
